//
//  ViewController.m
//  CALayerDemo
//
//  Created by ZJ on 2018/8/7.
//  Copyright © 2018年 iCatch Technology Inc. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, weak) CALayer *layer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
//    [self layerBaseAttr];
//    [self createLayerTest];
    [self layerTransformTest];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - CALayer 的 transform 属性
- (void)layerTransformTest {
    // 创建 layer
    CALayer *layer = [[CALayer alloc] init];
    
    layer.backgroundColor = [UIColor redColor].CGColor; // 颜色
    layer.position = CGPointMake(200, 200); // 位置
    layer.bounds = CGRectMake(0, 0, 100, 100); // 大小
    
    layer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"me"].CGImage);
    
    // 把 layer 添加到 控制器 view 的 layer 上
    [self.view.layer addSublayer:layer];
    
    // 给全局属性赋值
    self.layer = layer;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    // 旋转(z无效)
    self.layer.transform = CATransform3DRotate(self.layer.transform, M_PI_4, 0, 0, 1);
    
    // 缩放(z无效)
    self.layer.transform = CATransform3DScale(self.layer.transform, 1, 1, 0.5);
    
    // 平移(z无效)
    self.layer.transform = CATransform3DTranslate(self.layer.transform, 1, 1, 1);
}

#pragma mark - 手动创建CALayer
- (void)createLayerTest {
    // 创建 layer
    CALayer *layer = [[CALayer alloc] init];
    
    layer.backgroundColor = [UIColor redColor].CGColor; // 颜色
    layer.position = CGPointMake(200, 200); // 位置
    layer.bounds = CGRectMake(0, 0, 100, 100); // 大小
    
    // 把 layer 添加到 控制器 view 的 layer 上
    [self.view.layer addSublayer:layer];
    
    // 给全局属性赋值
    self.layer = layer;
}

/*
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    // self.layer.opacity = 0; // 透明度
    // self.layer.bounds = CGRectMake(0, 0, 200, 200);
    
    // 获取触摸对象
    UITouch *t = touches.anyObject;
    
    // 获取手指当前的位置
    CGPoint p = [t locationInView:t.view];
    
    // 禁用隐私动画(事务)
    [CATransaction begin]; // 开启事务
    [CATransaction setDisableActions:YES]; // 禁用隐式动画
    
    // 让layer 跑到手指的位置
    self.layer.position = p;
    
    [CATransaction commit]; // 提交事务
}
*/

#pragma mark - CALayer基本属性
- (void)layerBaseAttr {
    UIView *redView = [[UIView alloc] init];
    
    redView.frame = CGRectMake(100, 100, 100, 100);
    redView.backgroundColor = [UIColor redColor];
    
    // 1.边框
    redView.layer.borderWidth = 10; // 边框的宽度
    redView.layer.borderColor = [UIColor grayColor].CGColor; // 边框的颜色
    
    // 2.阴影
    redView.layer.shadowOffset = CGSizeMake(100, 100); // 阴影的偏移量
    redView.layer.shadowColor = [UIColor blueColor].CGColor; // 阴影的颜色
    redView.layer.shadowRadius = 50; // 阴影的半径
    redView.layer.shadowOpacity = 1; // 阴影的透明度
    
    // 3.圆角
    redView.layer.cornerRadius = 50; // 圆角半径
    redView.layer.masksToBounds = YES; // 裁剪超出 layer 范围的东西
    
    // 4.bounds
//    redView.layer.bounds = CGRectMake(0, 0, 200, 200);
    
    // 5.postion属性和view.center的关系 --> 默认 center 跑到 position 的位置上
//    redView.layer.position = CGPointMake(0, 0);
    
    // 6.设置内容(图片)
    redView.layer.contents = (__bridge id)([UIImage imageNamed:@"me"].CGImage);
    
    [self.view addSubview:redView];
    
    self.layer = redView.layer;
}

/*
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    // self.layer.opacity = 0; // 透明度
    // self.layer.bounds = CGRectMake(0, 0, 200, 200);
    
    // 获取触摸对象
    UITouch *t = touches.anyObject;
    
    // 获取手指当前的位置
    CGPoint p = [t locationInView:t.view];
    
    // 让layer 跑到手指的位置
    self.layer.position = p;
}
*/

@end
